<script setup lang="ts">
import { onMounted, ref, computed } from "vue";
import Player from "xgplayer";
import "xgplayer/dist/index.min.css";
import { useUser } from "@/store/useUser";
import defaultAvatar from "@/assets/app-mj.png";

const props = defineProps({
  msg: {
    type: Object,
    required: true,
  },
});
const userStore = useUser();

// 头像
const headSrc = computed(() => {
  if (isMine.value && userStore.getUserInfo.avatar) {
    return userStore.getUserInfo.avatar;
  }
  if (!isMine.value && props.msg.fromAvatar) {
    return props.msg.fromAvatar;
  }
  return defaultAvatar;
});
// 是不是自己的消息
const isMine = computed(() => {
  return props.msg.from === userStore.getUserInfo.uimKey;
});

const player = ref(null);
onMounted(() => {
  new Player({
    el: player.value, //元素id
    lang: "zh", //设置中文
    volume: 0, // 默认静音
    height: "300px",
    width: "200px",
    autoplay: false, //自动播放
    screenShot: false, // 开启截图功能
    //视频地址
    url: props.msg.content,
    //封面图
    poster: "",
    fluid: true, // 填满屏幕 （流式布局）
    playbackRate: [0.5, 1, 2], //传入倍速可选数组
  });
});
</script>

<template>
  <div class="text-msg">
    <el-image v-if="!isMine" class="head" :src="headSrc" alt="">
      <template #error>
        <div class="image-error">
          <img class="head" :src="defaultAvatar" alt="" />
        </div>
      </template>
    </el-image>
    <div class="msg-text" :class="{ mine: isMine }">
      <p class="name" v-if="!isMine">{{ props.msg.fromName }}</p>
      <span class="text" style="min-width: 400px; min-height: 200px">
        <div id="player" ref="player" style="width: 400px; height: 200px"></div>
      </span>
      <i v-if="isMine">{{ props.msg.read ? "已读" : "未读" }}</i>
    </div>
    <el-image v-if="isMine" class="head" :src="headSrc" alt="">
      <template #error>
        <div class="image-error">
          <img class="head" :src="defaultAvatar" alt="" />
        </div>
      </template>
    </el-image>
  </div>
</template>

<style scoped lang="scss">
.text-msg {
  display: flex;
  margin-bottom: 26px;
  .head {
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    margin-right: 8px;
    border-radius: 50%;
  }
  .msg-text {
    width: 100%;
    &.mine {
      text-align: right;
      padding-right: 10px;
      .text {
        // background: rgba(1, 154, 129, 1);
        color: #fff;
        border-radius: 12px 0 12px 12px;
      }
      i {
        display: block;
        font-style: normal;
        text-align: right;
        font-size: 12px;
        color: #999;
      }
    }
    .name {
      color: rgba(25, 30, 40, 0.36);
      font-size: 12px;
      margin-bottom: 4px;
    }
    .text {
      display: inline-block;
      background: #fff;
      padding: 12px 20px;
      border-radius: 0 12px 12px 12px;
      max-width: 80%;
      box-shadow: 0 4px 12px -2px rgba(11, 13, 18, 0.06);
    }
  }
}
</style>
